/*
 * Copyright (c) 2009 Marius Mathiesen <marius.mathiesen@gmail.com>
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
 */
package no.trafikanten;

import org.w3c.dom.*;
import java.util.Date;
import java.text.SimpleDateFormat;
/*
 * This DOM stuff makes me completely crazy
 * This class extracts the String value of what I understand to be a completely regular Node
 */
public class TextExtractor 
{
	Node node;
	public TextExtractor(Node node)
	{
		this.node = node;
	}
	
	public String extractStringValue()
	{
		String result = null;
		NodeList childList = this.node.getChildNodes(); 
		for (int x = 0; x < childList.getLength(); x++) 
		{ 
		    Node child = childList.item(x); 
		    if (child.getNodeType() == Node.TEXT_NODE) 
		    {
			    result = child.getNodeValue();
		    }
		} 
		return result;
	}
	
	public String toString()
	{
		return this.extractStringValue();
	}
	public int toInt()
	{
		return Integer.parseInt(toString());
	}
	public static Date parseDateString(String fullDateString) throws Exception
	{
		String dateString = fullDateString.substring(0, 18);
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");

		Date result = format.parse(dateString);
		return result;		
	}
	public Date toDate() throws Exception
	{
		return parseDateString(toString());
	}
	public String toDateString() throws Exception 
	{
		StringBuilder sb = new StringBuilder(this.toString());
		String result = sb.substring(11, 16);
		return result;			
	}
	
}
